/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author machine
 */
@Stateless
public class ColeccionpeliculasFacade extends AbstractFacade<Coleccionpeliculas> implements ColeccionpeliculasFacadeLocal {
    @PersistenceContext(unitName = "videosJEE-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ColeccionpeliculasFacade() {
        super(Coleccionpeliculas.class);
    }

    @Override
    public Integer consultarCantidad(Integer idcolecciones) {
        
        Long rowCnt = (Long) em.createNativeQuery("SELECT count(1) FROM coleccionpeliculas v where v.idcolecciones = "+idcolecciones+"  ").getSingleResult(); 
        
        return rowCnt.intValue();
        
    }

    @Override
    public List<Coleccionpeliculas> consultarVideos(String idcoleccion, String idvideo) {
        
        String condicion = "";
        
        if( idcoleccion != null ){
            condicion += " and v.idcolecciones.idcolecciones = " + idcoleccion + " ";
        }
        
        if( idvideo != null ){
            condicion += " and v.idpeliculas.idpeliculas = " + idvideo + " ";
        }
        
        Query query = em.createQuery( "SELECT v FROM Coleccionpeliculas v WHERE v.idcoleccionpeliculas > 0 " + condicion + " " );
        
        return query.getResultList(); // or getSingleResult();
        
    }
    
}
